package cn.bookcycle.paypage.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bookcycle.paypage.constant.Message;
import cn.bookcycle.paypage.util.DataFormatConvert;
import cn.bookcycle.paypage.util.DealInfo;

/**
 * 支付页面的后台
 *
 * @author liufenglin
 * @date 2018年3月15日
 */
@Controller
public class ReceiveNotify {
	private static final Logger logger = LoggerFactory.getLogger(ReceiveNotify.class);
	/**
	 * 接收支付微服务的通知消息
	 * 
	 * @return
	 */
	@RequestMapping(value = "listener", method = RequestMethod.POST)
	@ResponseBody
	public String reciveMsg(@RequestBody (required=false) String requestBody) {
		// 对接收到的数据进行URL解码
		try {
			requestBody = URLDecoder.decode(requestBody, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			logger.warn("listener 对接收到的数据进行解码异常！！！！");
		}
		// 清洗requestBody数据为标准的json
		requestBody = DataFormatConvert.washJson(requestBody);
		// 收到消息后，开一个线程去处理后续业务，以免耽搁回复支付微服务接口的时间，从未引起支付微服务多次回调
		DealInfo dealInfo = new DealInfo(requestBody);
		Thread thread = new Thread(dealInfo);
		thread.start();
		// 给微服务接口返回OK消息，避免三次发送消息
		return Message.OK;
	}
}
